<?php
$gameId = $_GET['gameId'];
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../../lib/layui-v2.8.12/src/layui.js"></script>
  <link rel="stylesheet" href="../../lib/layui-v2.8.12/src/css/layui.css">
  <script src="../../utils/form-serialize.js"></script>
  <script src="../../lib/axios/axios.min.js"></script>
  <link rel="stylesheet" href="../../css/common.css">
  <title>Document</title>
</head>

<body>
  <!-- 给容器追加 class="layui-form-pane"，即可显示为方框风格 -->
  <form class="layui-form layui-form-pane" id="add" action="./updateGame.php?gameId=<?php echo $gameId; ?>" method="POST" lay-filter="update">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" autocomplete="off" name="time" id="time" placeholder="yyyy-MM-dd HH:mm:ss" required>
        </div>
      </div>
    </div>

    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">比赛描述</label>
      <div class="layui-input-block">
        <textarea name="desc" maxlength="300" class="layui-textarea"></textarea>
      </div>
    </div>

    <div class="layui-form-item btn">
      <div>
        <button class="layui-btn" id="confirm">确认</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <script>
    layui.use('laydate', function() {
      //渲染时间选择器
      var laydate = layui.laydate;
      laydate.render({
        elem: '#time',
        type: 'datetime',
        fullPanel: true // 2.8+
      });
    })
  </script>
  <?php
  DEFINE('DB_USER', 'worldcup');
  DEFINE('DB_PASSWORD', '123456');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_NAME', 'worldcup');

  $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if (!$dbc) {
    die('Could not to connect to Mysql:' . mysqli_connect_error());
  }
  mysqli_set_charset($dbc, 'utf8');

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $time = $_POST['time'];
    $desc = $_POST['desc'];
    $gameId = $_GET['gameId'];

    $sqlUpdate = "update game set end_time='{$time}',comment='{$desc}',game_logo=2 where game_id=$gameId;";

    $resUpdate = mysqli_query($dbc, $sqlUpdate);
    if (!$resUpdate) {
      printf("Error: %s\n", mysqli_error($dbc));
      exit();
    }

    $sql1 = "call update_wins($gameId);";
    $sql2 = "call update_point ();";
    $sql3 = "call update_rank();";
    $resc = mysqli_query($dbc, $sql1);
    $resd = mysqli_query($dbc, $sql2);
    $rese = mysqli_query($dbc, $sql3);

    // echo $resUpdate;
    echo '<script>';
    if ($resUpdate == 1) {
      echo '
        layui.use(\'layer\', function() {
          var layer = layui.layer;
          layer.msg(\'修改比赛成功，请刷新页面\');
          setTimeout(() => {
            var index = parent.layer.getFrameIndex(window.name); // 获取弹出层索引
            parent.layer.close(index); // 关闭弹出层
          }, 2000);
        });
      ';
    } else {
      echo '
        layui.use(\'layer\', function() {
          var layer = layui.layer;
          layer.msg(\'修改比赛失败\');
          setTimeout(() => {
            var index = parent.layer.getFrameIndex(window.name); // 获取弹出层索引
            parent.layer.close(index); // 关闭弹出层
          }, 2000);
        });
      ';
    }
    echo '</script>';
  }
  ?>

</body>

</html>